package com.szeldon.declatest.exec;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

import com.szeldon.declatest.exec.events.TestSuiteExecEvent;
import com.szeldon.declatest.exec.events.dostep.jms.JmsMessageReceivedExecEvent;

/**
 * Container for history of test suite's execution.
 * 
 * @author szeldon
 * 
 */
public class TestSuiteExecHistory {

    private Logger logger = Logger.getLogger(TestSuiteExecHistory.class.getName());

    /** Map that contains scenario IDs as keys and list of execution events as value. */
    private HashMap<String, List<TestSuiteExecEvent>> historyMap = new HashMap<String, List<TestSuiteExecEvent>>();

    private String testSuiteId;

    public String getTestSuiteId() {
        return testSuiteId;
    }

    /**
     * Creates history container for given testSuiteName.
     * 
     * @param testSuiteId
     */
    public TestSuiteExecHistory(String testSuiteId) {
        this.testSuiteId = testSuiteId;

    }

    /**
     * Adds given event to list of events for scenario described in given event.
     * 
     * @param event
     */
    public void addExecEvent(TestSuiteExecEvent event) {

        logger.fine("Adding " + event + " to history.");

        String scenarioId = event.getScenarioId();

        List<TestSuiteExecEvent> eventDescriptionsList = null;
        if (historyMap.containsKey(scenarioId)) {
            eventDescriptionsList = historyMap.get(scenarioId);
        } else {
            eventDescriptionsList = new ArrayList<TestSuiteExecEvent>();
            historyMap.put(event.getScenarioId(), eventDescriptionsList);
        }

        eventDescriptionsList.add(event);

    }

    /**
     * Returns a content of response for given scenario step id.
     * 
     * @param scenarioStepId
     */
    public String getResponseContentForScenarioStep(String scenarioStepId) {
        String content = null;

        boolean shouldRun = true;

        for (String currentScenarioId : historyMap.keySet()) {
            for (TestSuiteExecEvent currentEvent : historyMap.get(currentScenarioId)) {
                if (currentEvent instanceof JmsMessageReceivedExecEvent) {
                    JmsMessageReceivedExecEvent responseEvent = (JmsMessageReceivedExecEvent) currentEvent;

                    String currentResponseEventScenarioId = responseEvent.getStepId();

                    if (currentResponseEventScenarioId.equals(scenarioStepId)) {
                        content = responseEvent.getMessageContent();
                        shouldRun = false;
                        break;
                    }

                }
            }
            if (!shouldRun) {
                break;
            }
        }

        return content;
    }

    /**
     * Gets a set of scenarios' ids. They can be used for getting events by using {@link #getEventsForScenario(String)}
     * method.
     * 
     * @return
     */
    public Set<String> getScenariosIds() {
        return historyMap.keySet();
    }

    /**
     * Returns a list of {@link TestSuiteExecEvent} objects that contain events from given scenario. If there are no
     * events for given scenarioName, null is returned.
     * 
     * @param scenarioId
     * @return
     */
    public List<TestSuiteExecEvent> getEventsForScenario(String scenarioId) {
        return historyMap.get(scenarioId);
    }
}
